package com.hue.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;

@Controller
public class FileController {
    @Value("${file.path}")
    private String filePath;

    @GetMapping("/index")
    public String index(){
        return "index";
    }

    @PostMapping("/upload")
    @ResponseBody
    public String upload(@RequestParam("file")MultipartFile file){
        //将文件保存到statics下
        String fileName = file.getOriginalFilename();
        try {
            file.transferTo(new File(filePath+"/"+fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "上传文件成功";
    }

    @PostMapping("/uploads")
    @ResponseBody
    public String uploads(@RequestParam("file")MultipartFile[] files){
        if(files==null || files.length==0){
            return "请选择文件";
        }
        try {
            for(MultipartFile file : files){
                String fileName = System.currentTimeMillis()+file.getOriginalFilename();
                file.transferTo(new File(filePath+"/"+fileName));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "上传多个文件成功";
    }

//    @GetMapping
//    public String download(){
//
//    }
}
